#include "omp.h"
#include <iostream>
#include <cstring>

using namespace std;
long num_steps = 100000;

int main() {
    double pi, sum = 0.0;
    double step = 1.0 / (double) num_steps;
#pragma omp parallel for
    for (int i = 0; i < num_steps; i++) {
        double x = (i + 0.5) * step;
        /*
         * critical下面的代码块中, 只能有一个线程同时进入
         */
#pragma omp critical
        {
            sum = sum + 4.0 / (1.0 + x * x);
        };

        /*
         * atomic只能保护一个内存地址(单个变量)的读和写,后面只能跟一条语句
         */
//#pragma omp atomic
//        sum = sum + 4.0 / (1.0 + x * x);


    }
    pi = step * sum;
    printf("%.10f\n", pi);
    return 0;
}
